package stack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/*
 * 32. 最长有效括号
 *
 *  利用栈, 开始时在栈中先存放一个-1;
 *
 * */
public class LongestValidParentheses32 {
    public int longestValidParentheses(String s) {
        int max=0;
        Stack<Integer> stack = new Stack<>();
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)=='('){
                stack.push(i);
            }else {
                stack.pop();
                if(stack.isEmpty()){
                    stack.push(i);
                }else {
                    max=Math.max(max,(i-stack.peek()));
                }
            }
        }
        return max;
    }
}

